package example.com.terry.utils;


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 字符编码过滤器
 * 创建人：ls
 * 时间：2020年5月22日-
 *
 * @version 1.0.0
 */
public class CharacterEncodingFilter implements Filter {

    private FilterConfig config;

    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        //获取字符编码配置参数
        String encoding = config.getInitParameter("encoding");
        //设置字符编码
        if (StringUtils.isNotBlank(encoding)) {
            request.setCharacterEncoding(encoding);
            response.setCharacterEncoding(encoding);
        }
        //把请求和响应传给过滤链中的下一个调用者或Servlet
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

}